//
//  ASDoutuHeadMenuView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASDoutuHeadMenuView.h"
#import "ASDoutuHeadModel.h"
static NSString *ASDoutuHeadMenuCellStr = @"ASDoutuHeadMenuCell";
@interface ASDoutuHeadMenuCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *mainImage;
@property (strong, nonatomic) UIView *shadowView;
@property (strong, nonatomic) UIView *borderView;
@property (strong, nonatomic) UILabel *titleLab;
@property (strong, nonatomic) ASDoutuHeadModel *model;
@end

@implementation ASDoutuHeadMenuCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.mainImage];
        [self.contentView addSubview:self.shadowView];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.borderView];
        [self setAllViewLayout];
    }
    return self;
}
- (void)setAllViewLayout{
    [self.borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}
- (UIView *)borderView{
    if (!_borderView) {
        _borderView = [UIView new];
        [_borderView setBackgroundColor:[UIColor clearColor]];
        _borderView.layer.masksToBounds = YES;
        _borderView.layer.cornerRadius = 5;
        _borderView.layer.borderColor = [UIColor colorWithHexString:@"#FF7785"].CGColor;
        _borderView.layer.borderWidth = 1;
    }
    return _borderView;
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [UIImageView new];
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
        _mainImage.layer.masksToBounds = YES;
        _mainImage.layer.cornerRadius = 5;
    }
    return _mainImage;
}

- (UIView *)shadowView{
    if (!_shadowView) {
        _shadowView = [UIView new];
        [_shadowView setBackgroundColor:[UIColor colorWithHexString:@"1a1a1a"]];
        _shadowView.layer.masksToBounds = YES;
        _shadowView.layer.cornerRadius = 5;
        _shadowView.alpha = 0.6;
    }
    return _shadowView;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.numberOfLines = 0;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = [UIFont systemFontOfSize:12];
        _titleLab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    }
    return _titleLab;
}

- (void)setModel:(ASDoutuHeadModel *)model{
    _model = model;
    [self.mainImage setImage:[UIImage imageNamed:model.imageStr]];
    self.titleLab.text = model.showName;
    if (model.ifSelect) {
        self.borderView.hidden = NO;
    }else{
        self.borderView.hidden = YES;
    }
}
@end

@implementation ASDoutuHeadMenuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(62);
        }];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.minimumLineSpacing = 6;
        fl.minimumInteritemSpacing = 6;
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fl.sectionInset = UIEdgeInsetsMake(0, 13, 0, 13);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ASDoutuHeadMenuCell class] forCellWithReuseIdentifier:ASDoutuHeadMenuCellStr];
    }
    return _collectionView;
}

- (void)setItemW:(CGFloat)itemW{
    _itemW = itemW;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(itemW);
    }];
}

- (void)setHeadArr:(NSArray *)headArr{
    _headArr = headArr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.headArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.itemW, self.itemW);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASDoutuHeadMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASDoutuHeadMenuCellStr forIndexPath:indexPath];
    if (self.headArr.count > indexPath.row) {
        cell.model = self.headArr[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self setScrollIndex:indexPath.row ifBlock:YES];
}

- (void)setScrollIndex:(NSInteger)pushIndex ifBlock:(BOOL)ifBlock{
    if (self.headArr.count > pushIndex) {
        for (ASDoutuHeadModel *subM in self.headArr) {
            subM.ifSelect = NO;
        }
        ASDoutuHeadModel *model = self.headArr[pushIndex];
        model.ifSelect = YES;
        [self.collectionView reloadData];
        if (ifBlock) {
            if (self.clickSubIndexBlock) {
                self.clickSubIndexBlock(pushIndex);
            }
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
